package com.ksd.modules.members.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.common.web.threadlocal.UserIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.members.entity.AdminUser;
import com.ksd.modules.members.entity.AdminUserWalletRecords;
import com.ksd.modules.members.mapper.AdminUserWalletRecordsMapper;
import com.ksd.modules.members.po.AdminUserWalletRecordsSavePO;
import com.ksd.modules.members.po.AdminUserWalletRecordsSearchPO;
import com.ksd.modules.members.service.IAdminUserService;
import com.ksd.modules.members.service.IAdminUserWalletRecordsService;
import com.ksd.modules.members.vo.AdminUserVo;
import com.ksd.modules.members.vo.AdminUserWalletRecordsVo;
import com.ksd.utils.snow.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminUserWalletRecordsServiceImpl extends BaseServiceImpl<AdminUserWalletRecordsMapper, AdminUserWalletRecords> implements IAdminUserWalletRecordsService {

    @Autowired
    private IAdminUserService adminUserService;
    /**
     * 查询列表
     * @param adminUserWalletRecordsSearchPO
     * @return
     */
    public Page<AdminUserWalletRecordsVo> findAdminUserWalletRecordss(AdminUserWalletRecordsSearchPO adminUserWalletRecordsSearchPO){
        Page<AdminUserWalletRecords> page = new Page<>(adminUserWalletRecordsSearchPO.getPageNo(),adminUserWalletRecordsSearchPO.getPageSize());
        LambdaQueryWrapper<AdminUserWalletRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminUserWalletRecords::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserWalletRecordsSearchPO.getPayMethod()), AdminUserWalletRecords::getPayMethod,adminUserWalletRecordsSearchPO.getPayMethod());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminUserWalletRecordsSearchPO.getOrderno()), AdminUserWalletRecords::getOrderno,adminUserWalletRecordsSearchPO.getOrderno());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminUserWalletRecordsSearchPO.getCode()), AdminUserWalletRecords::getCode,adminUserWalletRecordsSearchPO.getCode());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminUserWalletRecordsSearchPO.getUuid()), AdminUserWalletRecords::getUuid,adminUserWalletRecordsSearchPO.getUuid());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminUserWalletRecordsSearchPO.getPhone()), AdminUserWalletRecords::getPhone,adminUserWalletRecordsSearchPO.getPhone()); // 模糊匹配
        if(StringUtils.isNotEmpty(adminUserWalletRecordsSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like(
                            AdminUserWalletRecords::getTitle,adminUserWalletRecordsSearchPO.getKeyword())
                    .or().like(AdminUserWalletRecords::getNickname, adminUserWalletRecordsSearchPO.getKeyword())
                    .or().like(AdminUserWalletRecords::getUuid, adminUserWalletRecordsSearchPO.getKeyword())
                    .or().like(AdminUserWalletRecords::getUsername, adminUserWalletRecordsSearchPO.getKeyword())

            );
        }

        lambdaQueryWrapper.orderByDesc(AdminUserWalletRecords::getCreateTime);
        Page<AdminUserWalletRecords> adminUserWalletRecordsPage = this.page(page, lambdaQueryWrapper);
        Page<AdminUserWalletRecordsVo> adminUserWalletRecordsVoPage = tranferPageBo(adminUserWalletRecordsPage, AdminUserWalletRecordsVo.class);
        return adminUserWalletRecordsVoPage;
    }


    /**
     * 保存和更新
     * @param adminUserWalletRecordsSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public AdminUserWalletRecordsVo saveUpdateAdminUserWalletRecords(AdminUserWalletRecordsSavePO adminUserWalletRecordsSavePO){
        AdminUserWalletRecords adminUserWalletRecords = tranferBo(adminUserWalletRecordsSavePO, AdminUserWalletRecords.class);
        adminUserWalletRecords.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminUserWalletRecords.setSystemName(SystemIdThrealLocal.get().getSystemName());
        boolean b = this.saveOrUpdate(adminUserWalletRecords);
        AdminUserWalletRecordsVo adminUserWalletRecordsVo = tranferBo(adminUserWalletRecords, AdminUserWalletRecordsVo.class);
        return adminUserWalletRecordsVo;
    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    public AdminUserWalletRecordsVo getAdminUserWalletRecordsById(Long id){
        AdminUserWalletRecords adminUserWalletRecords = this.getById(id);
        AdminUserWalletRecordsVo adminUserWalletRecordsVo = tranferBo(adminUserWalletRecords, AdminUserWalletRecordsVo.class);
        return adminUserWalletRecordsVo;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminUserWalletRecordsById(Long id){
        boolean b = this.removeById(id);
        return b;
    }

    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminUserWalletRecords(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 用户绑定小册订单
     *
     * @param uuid
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean bindUserOrder(String uuid, String price) {
        AdminUserWalletRecords adminUserVipBuy = new AdminUserWalletRecords();
        AdminUserVo adminUserVo = adminUserService.getAdminUserByUUId(uuid);
        adminUserVipBuy.setUserId(adminUserVo.getId());
        adminUserVipBuy.setNickname(adminUserVo.getNickName());
        adminUserVipBuy.setUsername(adminUserVo.getUserName());
        adminUserVipBuy.setAvatar(adminUserVo.getAvatar());
        adminUserVipBuy.setUuid(adminUserVo.getUuid());
        adminUserVipBuy.setPhone(adminUserVo.getTelephone());
        adminUserVipBuy.setAddress(adminUserVo.getAddress());

        adminUserVipBuy.setTitle("后台人工充值");
        adminUserVipBuy.setDescription("后台人工充值");
        adminUserVipBuy.setCode(UserIdThrealLocal.get().getUserId()+"");
        adminUserVipBuy.setPrice(price);

        adminUserVipBuy.setOrderno(String.valueOf(new SnowflakeIdWorker(7, 8).nextId()));
        adminUserVipBuy.setTradeno(String.valueOf(new SnowflakeIdWorker(8, 8).nextId()));
        adminUserVipBuy.setPayMethod(4);
        adminUserVipBuy.setPayMethodName("人工");

        adminUserVipBuy.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminUserVipBuy.setSystemName(SystemIdThrealLocal.get().getSystemName());
        adminUserVipBuy.setOrderJson("");
        boolean b = this.saveOrUpdate(adminUserVipBuy);
        if (b) {
            AdminUser adminUser = new AdminUser();
            adminUser.setId(adminUserVo.getId());
            // 设置vip类型
            if (StringUtils.isEmpty(adminUserVo.getCoin())) {
                // 首冲
                adminUser.setCoin(price);
            } else {
                // 续充
                Integer newCoin = Integer.parseInt(adminUserVo.getCoin()) + Integer.parseInt(price);
                adminUser.setCoin(String.valueOf(newCoin));
            }
            // 同步和修改
            adminUserService.updateById(adminUser);
        }
        return b;
    }
}
